package com.ymatou.iapi.productprice.testcase;

import com.google.common.reflect.TypeToken;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productprice.parameter.GetGrouponPriceByProdIdBean;
import com.ymatou.iapi.productprice.parameter.GrouponPrice;
import com.ymatou.iapi.productprice.service.GetGrouponPriceByProdIdCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.stream.Collectors;

/**
*获取商品拼团价
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetGrouponPriceByProdId{
	private static GetGrouponPriceByProdIdBean getgrouponpricebyprodidBean;
	private static GetGrouponPriceByProdIdCall getgrouponpricebyprodidCall;
	private static YmtProductsIWapper ymtProductsIWapper;
	private YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

	/**
	 * 优选
	 */
	private static Integer sellerId = 3383;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品拼团价");
	}

	@Before
	public void caseUp() {
		getgrouponpricebyprodidBean=new GetGrouponPriceByProdIdBean();
		getgrouponpricebyprodidCall=new GetGrouponPriceByProdIdCall();
		ymtProductsIWapper = new YmtProductsIWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getgrouponpricebyprodidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
	}

	private void setPrice(AddSellerProductRequest addpro, Double price) {
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setNewPrice(price);
			catalog.setMarketPrice(price);
			catalog.setVipPrice(price);
			catalog.setPrice(price);
		});
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetGrouponPriceByProdId_01() {
		Logger.start(true,"获取商品拼团价");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			Double groupPrice = 50d;
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,pid,groupPrice);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			List<GrouponProducts> gp = ymtProductsIWapper.selectGrouponProducts(pid, null, 0);

			
			getgrouponpricebyprodidBean.setProductId(pid);
			getgrouponpricebyprodidBean.setGrouponVersion(Integer.parseInt(gp.get(0).getVersion().toString()));
			getgrouponpricebyprodidCall.setData(getgrouponpricebyprodidBean);
			getgrouponpricebyprodidCall.callService();
			Logger.verifyEquals("200",getgrouponpricebyprodidCall.getString("Code"),"验证Code");
			String CatalogGrouponPriceList = getgrouponpricebyprodidCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogGrouponPriceList").getAsJsonArray().toString();
			List<GrouponPrice> CatalogPriceList=(List<GrouponPrice>) FormatUtil.GSONToObject(CatalogGrouponPriceList, new TypeToken<List<GrouponPrice>>(){}.getType());
			CatalogPriceList.forEach(p->{
				for (int i=0;i<CatalogPriceList.size();i++){
					if (p.getCatalogId().equals(catalogs.get(i))){
						Logger.verifyEquals(p.getGrouponPrice(),groupPrice,"验证groupPrice");
					}
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetGrouponPriceByProdId_02() {
		Logger.start(true,"获取商品拼团价，版本号已经过期");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			Double groupPrice = 50d;
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,pid,groupPrice);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			List<GrouponProducts> gp = ymtProductsIWapper.selectGrouponProducts(pid, null, 0);

			ProductManagerCallServiceV3.StopGroupon(gp.get(0).getGrouponProductId());
			getgrouponpricebyprodidBean.setProductId(pid);
			getgrouponpricebyprodidBean.setGrouponVersion(Integer.parseInt(gp.get(0).getVersion().toString()));
			getgrouponpricebyprodidCall.setData(getgrouponpricebyprodidBean);
			getgrouponpricebyprodidCall.callService();
			Logger.verifyEquals("200",getgrouponpricebyprodidCall.getString("Code"),"验证Code");
			String CatalogGrouponPriceList = getgrouponpricebyprodidCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogGrouponPriceList").getAsJsonArray().toString();
			List<GrouponPrice> CatalogPriceList=(List<GrouponPrice>) FormatUtil.GSONToObject(CatalogGrouponPriceList, new TypeToken<List<GrouponPrice>>(){}.getType());
			CatalogPriceList.forEach(p->{
				for (int i=0;i<CatalogPriceList.size();i++){
					if (p.getCatalogId().equals(catalogs.get(i))){
						Logger.verifyEquals(p.getGrouponPrice(),groupPrice,"验证groupPrice");
					}
				}
			});


		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetGrouponPriceByProdId_03() {
		Logger.start(true,"获取商品拼团价，多规格价格");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			setStock(addpro, 30);
			setPrice(addpro, 100d);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
			SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,pid,50d);
			String catalog1 = saveGrouponBean.getGrouponCatalogs().get(1).getCatalogId();
			saveGrouponBean.getGrouponCatalogs().get(1).setGrouponPrice(60d);
			ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
			List<GrouponProducts> gp = ymtProductsIWapper.selectGrouponProducts(pid, null, 0);

			getgrouponpricebyprodidBean.setProductId(pid);
			getgrouponpricebyprodidBean.setGrouponVersion(Integer.parseInt(gp.get(0).getVersion().toString()));
			getgrouponpricebyprodidCall.setData(getgrouponpricebyprodidBean);
			getgrouponpricebyprodidCall.callService();
			Logger.verifyEquals("200",getgrouponpricebyprodidCall.getString("Code"),"验证Code");
			String CatalogGrouponPriceList = getgrouponpricebyprodidCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogGrouponPriceList").getAsJsonArray().toString();
			List<GrouponPrice> CatalogPriceList=(List<GrouponPrice>) FormatUtil.GSONToObject(CatalogGrouponPriceList, new TypeToken<List<GrouponPrice>>(){}.getType());
			CatalogPriceList.forEach(p->{
				for (int i=0;i<CatalogPriceList.size();i++){
					if (p.getCatalogId().equals(catalog1)){
						Logger.verifyEquals(p.getGrouponPrice(),60d,"验证groupPrice");
					}else{
						Logger.verifyEquals(p.getGrouponPrice(),50d,"验证groupPrice");
					}
				}
			});


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}